package cn.hyy.hrm.util;

import cn.hyy.hrm.config.SMSConst;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public enum SMSUtil {

    INSTANCE;

    /**
     * 发送短信   两种方式：1、调用第三方封装好的SDK  2、发送请求调用REST接口
     * @param phoneNum 手机号码
     * @param signName 签名
     * @param templateCode 模板
     * @param templateParam 模板参数
     */
    public void sendSMS(String phoneNum,String signName,String templateCode,String templateParam) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", SMSConst.ACCESS_KEY, SMSConst.SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam);
        CommonResponse response = client.getCommonResponse(request);

        //处理响应内容
        System.out.println(response.getData());
    }

}
